package com.prs.crm.action.oa;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.oa.Memo;
import com.prs.crm.service.oa.MemoService;

public class MemoAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private Memo memo;
	private MemoService memoService;
	private PaginationSupport pagination;
	private Integer[] memoIds;
	private String currentDate;
	
	@Action("listMemo")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Memo.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMemoService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action(value="dealMemo",results={@Result(name="success",type="redirect",location="listMemo"),
			@Result(name="error",type="chain",location="listMemo")})
	public String dealMemo() {
		if(memoIds == null) {
			this.addActionError("请选择要处理的备忘");
			return ERROR;
		}
		this.getMemoService().dealMemo(memoIds);
		return SUCCESS;
	}

	@Action("editMemo")
	public String edit() {
		this.setCurrentDate(ToolKits.currentDate());
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="saveMemo",results={@Result(name="success",type="redirect",location="listMemo"),
			@Result(name="error",type="chain",location="editMemo")})
	public String saveMemo() {		
		if(memo.getTitle().equals("")) {
			this.addActionError("标题不能为空");
			return ERROR;
		}
		memo.setState(Constrains.MEMO_NOTDEAL);
		this.getMemoService().save(memo);
		this.setPrompt("提交备忘录成功");
		return SUCCESS;
	}
	
	@Action(value="removeMemo",results={@Result(name="success",type="redirect",location="listMemo")})
	public String removeMemo() {
		this.getMemoService().batchRemove(memoIds);
		return SUCCESS;
	}
	
	@Action("viewMemo")
	public String viewMemo() {
		return SUCCESS;
	}
	
	public Memo getMemo() {
		return memo;
	}
	public void setMemo(Memo memo) {
		this.memo = memo;
	}
	public MemoService getMemoService() {
		return memoService;
	}
	public void setMemoService(MemoService memoService) {
		this.memoService = memoService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getMemoIds() {
		return memoIds;
	}
	public void setMemoIds(Integer[] memoIds) {
		this.memoIds = memoIds;
	}

	public void setCurrentDate(String currentDate) {
		this.currentDate = currentDate;
	}

	public String getCurrentDate() {
		return currentDate;
	}
	
}
